<template>
  <div class="address-container">
    <h2>收货地址管理</h2>
    <div v-if="addresses.length === 0">暂无收货地址</div>
    <div class="address-list">
      <div v-for="addr in addresses" :key="addr.id" class="address-card">
        <img :src="addr.img || '/src/assets/tea/longjing.jpg'" alt="地址图片" />
        <div class="address-info">
          <div class="address-detail">{{ addr.detail }}</div>
          <div class="address-user">收件人：{{ addr.username }}</div>
          <div class="address-phone">电话：{{ addr.phone }}</div>
          <button @click="removeAddress(addr.id)">删除</button>
        </div>
      </div>
    </div>
    <form @submit.prevent="addAddress" class="address-form">
      <input v-model="detail" placeholder="详细地址" required />
      <input v-model="username" placeholder="收件人" required />
      <input v-model="phone" placeholder="电话" required />
      <button type="submit">添加地址</button>
    </form>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
const addresses = ref<any[]>([]);
const detail = ref('');
const username = ref('');
const phone = ref('');
const removeAddress = (id: string) => {
  addresses.value = addresses.value.filter(a => a.id !== id);
};
const addAddress = () => {
  addresses.value.push({
    id: Date.now(),
    detail: detail.value,
    username: username.value,
    phone: phone.value,
    img: '/src/assets/tea/longjing.jpg'
  });
  detail.value = '';
  username.value = '';
  phone.value = '';
};
</script>

<style scoped>
.address-container {
  max-width: 700px;
  margin: 40px auto;
  padding: 32px;
  background: #fffbe6;
  border-radius: 16px;
  box-shadow: 0 2px 16px #ffd591;
}
.address-list {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  justify-content: flex-start;
  margin-bottom: 24px;
}
.address-card {
  display: flex;
  align-items: center;
  width: 400px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px #ffd591;
  padding: 16px;
  margin-bottom: 16px;
}
.address-card img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 8px;
  margin-right: 16px;
}
.address-info {
  flex: 1;
}
.address-detail {
  font-size: 15px;
  font-weight: bold;
  margin-bottom: 4px;
}
.address-user, .address-phone {
  font-size: 14px;
  margin-bottom: 2px;
}
.address-card button {
  background: #ff6600;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 6px 16px;
  cursor: pointer;
}
.address-form {
  display: flex;
  gap: 12px;
  margin-top: 24px;
}
.address-form input {
  padding: 8px 12px;
  border-radius: 4px;
  border: 1px solid #ffd591;
}
.address-form button {
  background: #ff6600;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  cursor: pointer;
}
</style>
